package cn.chinaunicom.sdsi.gacx.sys.service.serviceImpl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.chinaunicom.sdsi.frm.base.BaseService;
import cn.chinaunicom.sdsi.frm.sys.R;
import cn.chinaunicom.sdsi.frm.sys.entity.SysStaff;
import cn.chinaunicom.sdsi.frm.sys.entity.SysStaffOrg;
import cn.chinaunicom.sdsi.gacx.sys.dao.mybatis.GaMainInfoMapper;
import cn.chinaunicom.sdsi.gacx.sys.dao.mybatis.GaSysParamMapper;
import cn.chinaunicom.sdsi.gacx.sys.dao.mybatis.GaSysSelectLogMapper;
import cn.chinaunicom.sdsi.gacx.sys.entity.GaSysParam;
import cn.chinaunicom.sdsi.gacx.sys.entity.GaSysSelectLog;
import cn.chinaunicom.sdsi.gacx.sys.service.GaSysParamService;
import cn.chinaunicom.sdsi.gacx.sys.service.GaSysSelectLogService;
import cn.chinaunicom.sdsi.gacx.sys.service.SysMainService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * @author chenxl 20170619
 *
 */
@Service
public class GaSysSelectLogServiceImpl extends BaseService implements GaSysSelectLogService {

	@Autowired
	private GaSysSelectLogMapper gaSysSelectLogMapper;

	/**
	 * 统一日志保存服务
	 */
	@Override
	public JSONObject saveSysSelectLog(Map requestMap) {
		JSONObject rObj = new JSONObject();
		GaSysSelectLog log = new GaSysSelectLog();

		String staffId = (String) requestMap.get("staffId");
		String funcCode = (String) requestMap.get("funcCode");
		String elecId = (String) requestMap.get("elecId");
		String selectType = (String) requestMap.get("selectType");
		String ipAddr = (String) requestMap.get("ipAddr");
		String tradeId = getTradeId();
		log.setStaffId(staffId);
		log.setFuncCode(funcCode);
		log.setElecId(elecId);
		log.setSelectType(selectType);
		log.setIpAddr(ipAddr);
		log.setOperDate(new Date());
		log.setId(tradeId);
		gaSysSelectLogMapper.addGaSelectLog(log);
		

		String idNos =  (String) requestMap.get("idNo");
		String phoneNos =  (String) requestMap.get("phoneNo");
		//获得公函校验中的身份证号
		List numList = new ArrayList();
		if(idNos!=null){
			for(String idNo : idNos.split(",")){
				numList.add(idNo);
			}
		}
		//获得公函校验中的电话
		if(phoneNos!=null){
			for(String phoneNo : phoneNos.split(",")){
				numList.add(phoneNo);
			}
		}
		if(numList.size()>0){
			gaSysSelectLogMapper.addGaSelectNumLog(tradeId, numList);
		}
		return rObj;
	}

	private String getTradeId() {
		Random r = new Random();
		Integer max = 999999;
		Integer min = 100000;
		Integer rondom = r.nextInt(max) % (max - min + 1) + min;
		SimpleDateFormat df3 = new SimpleDateFormat("yyyyMMddHHmmss");// 设置日期格式
		String tradeId = df3.format(new Date()) + rondom;
		return tradeId;
	}

}